// pages/search/search.js
const app = getApp()
import config from '../../utils/config.js';
import util from '../../utils/util.js';
import {
  indexModel
} from '../../models/index';
const index = new indexModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '搜索',
      //  导航栏 中间的标题
    },
    height: app.globalData.height,
    navHeight: app.globalData.barheight,
    ifFocus:false,

    searchVal:'',  // 查询内容
    userId:'',     // 用户id
    type:'',       // 类型 1 商品 3 档口 4 食堂人员
    searchName:'', // 父级查询

    hotlist:[],    // 热搜列表
    searchList:[], // 历史列表

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync('userInfo')
    var userId = userInfo.id
    var searchName = '';
    if(options.type==3){
      searchName = "gateSealist";
    }else if(options.type==4){
      searchName = "persSealist";
    }else if(options.type==1){
      searchName = "goodSealist";
    }
    this.setData({
      type:options.type,
      userId:userId,
      searchName:searchName,
      searchList: wx.getStorageSync(searchName+userId) || [],
    })
    this.hotSearch();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
        ifFocus:true
      })
  },

  // 热搜查询
  hotSearch(){
    index.apiHotSearch({
      page:1,
      limit:10,
      type:this.data.type
    }, res => {
      this.setData({
        hotlist:res.list
      })
    })
  },

  // 搜一搜
  searchInput(event){
    this.setData({
      searchVal:event.detail.value
    })
  },

  // 搜索
  search(e){
    if(!this.data.searchName){
      return
    }
    var val =  e.currentTarget.dataset.val;
    var getVal = '';
    if (this.data.searchVal) {
      getVal = this.data.searchVal
    }
    if(val){
      getVal = val;
    }
    if(getVal){
      var sealist = this.data.searchList;
      for (var i = 0; i < sealist.length; i++) {
        if (getVal == sealist[i]) {
          sealist.splice(i, 1)
          break;
        }
      }
      sealist.unshift(getVal)
      if(sealist.length>8){
        sealist.pop()
      }
      wx.setStorageSync(this.data.searchName + this.data.userId, sealist)
      this.setData({
        searchList: sealist
      })
    }

    // 搜索跳转
    this.jumpPage(getVal)
    
  },

  // 搜索跳转结果页面
  jumpPage(getVal){
    var type = this.data.type;
    var url = '';
    if(type==3){
      url = '/pages/coreAdmin/gate/gateSearch/gateSearch?val='+getVal
    }else if(type==4){
      url = '/pages/coreAdmin/personnel/personnelSearch/personnelSearch?val='+getVal
    }else if(type==1){
      url = 'searchList/searchList?val='+getVal
    }
    if(!url){
      return
    }
    wx.navigateTo({
      url: url
    });
  },

  deleteAll(){
    wx.setStorageSync(this.data.searchName + this.data.userId, [])
    this.setData({
      searchList: []
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})